using System;
using System.Collections.Generic;
using System.Web;
using SpiderMaster.AspNet.TradingSystem.DataAccessLayer;

namespace SpiderMaster.AspNet.TradingSystem
{
    public class CachedMember
    {
        #region Locals

        private int _MemberId;
        private string _UserName;
        private DateTime _MemberSince;
        private DateTime _MemberSinceUtc;
        private Int32 _FavouriteSearchLocationId;
        private Int32 _FeedBackPossitive;
        private Int32 _FeedBackNeutral;
        private Int32 _FeedBackNegative;
        private String _BuyerEmail;
        private Boolean _AutoSendBuyerEmail;
        private Decimal _AccountBalance;
        private Int32 _CreatedListingsCurrent;
        private Int32 _CreatedListingsTotal;
        private Int32 _WatchedListingsCurrent;
        private Int32 _WatchedListingsTotal;
        private Int32 _CreatedListingsSuccessCurrent;
        private Int32 _CreatedListingsSuccessTotal;
        private Int32 _CreatedListingsFailedCurrent;
        private Int32 _CreatedListingsFailedTotal;
        private Int32 _WatchedListingsSuccessCurrent;
        private Int32 _WatchedListingsSuccessTotal;
        private Int32 _WatchedListingsFailedCurrent;
        private Int32 _WatchedListingsFailedTotal;

        #endregion

        #region Read Only

        private int MemberId
        {
            get { return _MemberId; }
        }
        private string UserName
        {
            get
            {
                return _UserName;
            }
        }
        private DateTime MemberSince
        {
            get
            {
                return _MemberSince;
            }
        }
        private DateTime MemberSinceUtc
        {
            get
            {
                return _MemberSinceUtc;
            }
        }
        private Int32 FavouriteSearchLocationId
        {
            get
            {
                return _FavouriteSearchLocationId;
            }
        }
        private Int32 FeedBackPossitive
        {
            get
            {
                return _FeedBackPossitive;
            }
        }
        private Int32 FeedBackNeutral
        {
            get
            {
                return _FeedBackNeutral;
            }
        }
        private Int32 FeedBackNegative
        {
            get
            {
                return _FeedBackNegative;
            }
        }
        private String BuyerEmail
        {
            get
            {
                return _BuyerEmail;
            }
        }
        private Boolean AutoSendBuyerEmail
        {
            get
            {
                return _AutoSendBuyerEmail;
            }
        }
        private Decimal AccountBalance
        {
            get
            {
                return _AccountBalance;
            }
        }
        private Int32 CreatedListingsCurrent
        {
            get
            {
                return _CreatedListingsCurrent;
            }
        }
        private Int32 CreatedListingsTotal
        {
            get
            {
                return _CreatedListingsTotal;
            }
        }
        private Int32 WatchedListingsCurrent
        {
            get
            {
                return _WatchedListingsCurrent;
            }
        }
        private Int32 WatchedListingsTotal
        {
            get
            {
                return _WatchedListingsTotal;
            }
        }
        private Int32 CreatedListingsSuccessCurrent
        {
            get
            {
                return _CreatedListingsSuccessCurrent;
            }
        }
        private Int32 CreatedListingsSuccessTotal
        {
            get
            {
                return _CreatedListingsSuccessTotal;
            }
        }
        private Int32 CreatedListingsFailedCurrent
        {
            get
            {
                return _CreatedListingsFailedCurrent;
            }
        }
        private Int32 CreatedListingsFailedTotal
        {
            get
            {
                return _CreatedListingsFailedTotal;
            }
        }
        private Int32 WatchedListingsSuccessCurrent
        {
            get
            {
                return _WatchedListingsSuccessCurrent;
            }
        }
        private Int32 WatchedListingsSuccessTotal
        {
            get
            {
                return _WatchedListingsSuccessTotal;
            }
        }
        private Int32 WatchedListingsFailedCurrent
        {
            get
            {
                return _WatchedListingsFailedCurrent;
            }
        }
        private Int32 WatchedListingsFailedTotal
        {
            get
            {
                return _WatchedListingsFailedTotal;
            }
        }

        #endregion

        private CachedMember()
        {
        }

        public CachedMember(int MemberId, string UserName, Int32 FavouriteSearchLocationId, Int32 FeedBackPossitive, Int32 FeedBackNeutral, Int32 FeedBackNegative, String BuyerEmail, Boolean AutoSendBuyerEmail, Decimal AccountBalance, Int32 CreatedListingsCurrent, Int32 CreatedListingsTotal, Int32 WatchedListingsCurrent, Int32 WatchedListingsTotal, Int32 CreatedListingsSuccessCurrent, Int32 CreatedListingsSuccessTotal, Int32 CreatedListingsFailedCurrent, Int32 CreatedListingsFailedTotal, Int32 WatchedListingsSuccessCurrent, Int32 WatchedListingsSuccessTotal, Int32 WatchedListingsFailedCurrent, Int32 WatchedListingsFailedTotal)
        {
            this._MemberId = MemberId;
            this._UserName = UserName;
            this._MemberSince = MemberSince;
            this._MemberSinceUtc = MemberSinceUtc;
            this._FavouriteSearchLocationId = FavouriteSearchLocationId;
            this._FeedBackPossitive = FeedBackPossitive;
            this._FeedBackNeutral = FeedBackNeutral;
            this._FeedBackNegative = FeedBackNegative;
            this._BuyerEmail = BuyerEmail;
            this._AutoSendBuyerEmail = AutoSendBuyerEmail;
            this._AccountBalance = AccountBalance;
            this._CreatedListingsCurrent = CreatedListingsCurrent;
            this._CreatedListingsTotal = CreatedListingsTotal;
            this._WatchedListingsCurrent = WatchedListingsCurrent;
            this._WatchedListingsTotal = WatchedListingsTotal;
            this._CreatedListingsSuccessCurrent = CreatedListingsSuccessCurrent;
            this._CreatedListingsSuccessTotal = CreatedListingsSuccessTotal;
            this._CreatedListingsFailedCurrent = CreatedListingsFailedCurrent;
            this._CreatedListingsFailedTotal = CreatedListingsFailedTotal;
            this._WatchedListingsSuccessCurrent = WatchedListingsSuccessCurrent;
            this._WatchedListingsSuccessTotal = WatchedListingsSuccessTotal;
            this._WatchedListingsFailedCurrent = WatchedListingsFailedCurrent;
            this._WatchedListingsFailedTotal = WatchedListingsFailedTotal;
        }
    }

    public sealed class MemberCache
    {
        internal const string AllMembersKey = "MemberCache_AllMembersKey";
        private static readonly string[] AllMembersKeyArrayWrap = new string[] { AllMembersKey };

        private MemberCache()
        {
        }
        public static void ClearAll(HttpContext context)
        {
            context.Cache.Remove(AllMembersKey);
        }

        public static List<CachedMember> GetAllMembers()
        {
            return GetAllMembers(HttpContext.Current);
        }
        public static List<CachedMember> GetAllMembers(HttpContext Context)
        {
            if (Context == null)
                return null;

            DateTime ExpirationDateTime = DateTime.Now.AddDays(1);
            List<CachedMember> AllMembers = Context.Cache[AllMembersKey] as List<CachedMember>;
            if (AllMembers == null)
            {
                AllMembers = FetchAllMembers();
                Context.Cache.Add(AllMembersKey, AllMembers, null, ExpirationDateTime, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            }
            return AllMembers;
        }
        private static List<CachedMember> FetchAllMembers()
        {
            // TODO:
            List<CachedMember> List = new List<CachedMember>();
            //MembersDataComponent.MembersDataTable MemberResults = MemberManagement.DB.Get.All();
            //foreach (MembersDataComponent.MembersRow MemberResult in MemberResults)
            //    List.Add(new CachedMember(MemberResult.Id, MemberResult.UserName, MemberResult.FavouriteSearchLocationId, MemberResult.FeedBackPossitive, MemberResult.FeedBackNeutral, MemberResult.FeedBackNegative, MemberResult.BuyerEmail, MemberResult.AutoSendBuyerEmail, MemberResult.AccountBalance, MemberResult.CreatedListingsCurrent, MemberResult.CreatedListingsTotal, MemberResult.WatchedListingsCurrent, MemberResult.WatchedListingsTotal, MemberResult.CreatedListingsSuccessCurrent, MemberResult.CreatedListingsSuccessTotal, MemberResult.CreatedListingsFailedCurrent, MemberResult.CreatedListingsFailedTotal, MemberResult.CreatedListingsSuccessCurrent, MemberResult.WatchedListingsSuccessTotal, MemberResult.WatchedListingsFailedCurrent, MemberResult.WatchedListingsFailedTotal));
            return List;
        }
    }
}